<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CatererController
 *
 * @author simon.zwicker
 */
class CatererController extends Zend_Controller_Action
{
    public function init()
    {
        if ($_SESSION['logged'] != TRUE) {
            header('location: /index/login');
        }
        parent::init();
    }
    
    protected function getCatForm()
    {
        $form = new Zend_Form;
        $form->setAction('/caterer/checkcaterer')->setMethod('post');
        
        $name = new Zend_Form_Element_Text('caterer_name',array('placeholder' => 'Name des Caterers'));
        $address = new Zend_Form_Element_Text('caterer_address',array('placeholder' => 'Anschrift'));
        $plz = new Zend_Form_Element_Text('plz',array('placeholder' => 'Postleitzahl'));
        $city = new Zend_Form_Element_Text('city',array('placeholder' => 'Ort'));
        $phone = new Zend_Form_Element_Text('phone',array('placeholder' => 'Telefonnummer'));
        $link = new Zend_Form_Element_Text('cardlink',array('placeholder' => 'Link zur Speisekarte'));
        
        $form->addElement($name)
            ->addElement($address)
            ->addElement($plz)
            ->addElement($city)
            ->addElement($phone)
            ->addElement($link)
            ->addElement('submit','caterer_create',array('label' => 'Caterer erstellen'));
        
        return $form;
    }
    
    public function createAction()
    {
        if ($_SESSION['admin'] != 1) {
            header('location: /index/index');
        }
        $this->view->form = $this->getCatForm();
    }
    
    public function checkcatererAction()
    {
        //DB
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini','staging');
        Zend_Registry::set('config',$config);

        $db = Zend_Db::factory($config->db);
        Zend_Registry::set('db',$db);
        
        $cat_param = $this->getRequest()->getParams();
        
        if (!empty($cat_param['caterer_name']) && !empty($cat_param['caterer_address']) && !empty($cat_param['plz']) && !empty($cat_param['city']) && !empty($cat_param['phone']) && !empty($cat_param['cardlink'])) {
            $data = $db->fetchAll('SELECT * FROM adresse WHERE name = "' . $cat_param['caterer_name'] . '"');
            if (!$data) {
                $db->insert('adresse', array(
                   'name' => $cat_param['caterer_name'],
                   'address' => $cat_param['caterer_address'],
                   'plz' => $cat_param['plz'],
                   'city' => $cat_param['city'],
                   'phone' => $cat_param['phone']
                ));
                
                $data = $db->fetchAll('SELECT * FROM adresse WHERE name = "' . $cat_param['caterer_name'] . '"');

                $db->insert('caterer',array(
                    'address_id' => $data[0]['address_id'],
                    'card' => $cat_param['cardlink']
                ));
                
                $this->view->msgcat = 'Der Caterer wurde angelegt und es kann <a href="/order/order">Bestellt</a> werden';
            } else {
                $this->view->msgcat = 'Diesen Caterer gibt es bereits!<br /><br /><a href="javascript:back()">Zurück</a>';
            }
        } else {
            $this->view->msgcat = 'Bitte alle Felder ausfüllen';
        }
    }
}

